package com.huadao.cart_ccs.main.login;

import com.huadao.cart_ccs.common.ProjectManager;
import com.huadao.cart_ccs.common.UtilHelpers;
import com.huadao.cart_ccs.data.bean.DeviceInfo;
import com.huadao.cart_ccs.data.source.http.RockBaseObserver;
import com.huadao.cart_ccs.greendao.entity.Project;
import com.huadao.cart_ccs.modle.DeviceInfoModle;
import com.huadao.cart_ccs.modle.ProjectModle;

import io.reactivex.Observable;

public class LoginPresenter extends LoginContract.ILoginPresenter {

    private DeviceInfoModle deviceInfoModle;
    private ProjectModle projectModle;
    private boolean isDeviceCreated;
    private String loginCode;

    public LoginPresenter(LoginContract.ILoginView baseView) {
        attachView(baseView);
        deviceInfoModle=new DeviceInfoModle(baseView.getContext());
        projectModle=new ProjectModle(baseView.getContext());
    }

    @Override
    void createDevice() {
        Observable<DeviceInfo> observable = deviceInfoModle.createAsync(UtilHelpers.getMAC());
        deviceInfoModle.toSubscribe(observable, new RockBaseObserver<DeviceInfo>(this, "正在创建设备") {
            @Override
            protected void onBaseNext(DeviceInfo deviceInfo) {
                isDeviceCreated=true;
                baseView.createDeviceResult(deviceInfo);
            }
            @Override
            protected void onBaseError(Throwable t) {
                baseView.createDeviceResult(null);
            }
        });
    }

    @Override
    public void queryProject(String devId) {
        Observable<com.huadao.cart_ccs.data.bean.Project> observable = projectModle.getProjectByDevid(devId,getLoginCode());
        projectModle.toSubscribe(observable, new RockBaseObserver<com.huadao.cart_ccs.data.bean.Project>(this, null) {
            @Override
            protected void onBaseNext(com.huadao.cart_ccs.data.bean.Project project) {
                baseView.onQueryProjectResult(project);
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                baseView.onQueryProjectResult(null);
            }
        });
    }

    @Override
    public void login() {
//        String account=baseView.getUserName();
//        String pwd=baseView.getEncryptedPwd();
//        Observable<User> observable=loginModle.login(account,pwd);
//        loginModle.toSubscribe(observable,new RockBaseObserver<User>(this,"正在登陆") {
//            @Override
//            protected void onBaseNext(User data) {
//                baseView.onLoginResult();
//            }
//        });
        baseView.onLoginResult();
    }

    public void checkLastProject() {
        Project project = ProjectManager.getInstance().getLastProject();
        if (project!=null&&project.getEndTime() == null) {
            baseView.onCheckLastProject(project);
        } else {
            baseView.onCheckLastProject(null);
        }
    }

    @Override
    public synchronized String  getLoginCode() {
        if(loginCode==null){
            loginCode=ProjectModle.generateLoginCode();
//            loginCode="583510";
        }
        return loginCode;
    }

    public void reInit() {
        deviceInfoModle=new DeviceInfoModle(baseView.getContext());
        projectModle=new ProjectModle(baseView.getContext());
    }
}
